#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QtDeclarative>
#include "internetclass.h"
#include "processadorclass.h"
#include "trackmodel.h"
#include "bancocare.h"
#include "detalhemodel.h"
#include "cutil.h"

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QTranslator tradutor;
    if (QLocale::system().name() != "pt_BR") {
        if (tradutor.load("CheckMyOrders_en", ":/")) {
            app->installTranslator(&tradutor);
        }
    } else {
        if (tradutor.load("CheckMyOrders_br", ":/")) {
            app->installTranslator(&tradutor);
        }
    }
    QScopedPointer<InternetClass> internet(new InternetClass());
    QScopedPointer<ProcessadorClass> processador(new ProcessadorClass());
    QScopedPointer<TrackModel> trackModel(new TrackModel());
    QScopedPointer<BancoCare> bancoCare(new BancoCare());
    QScopedPointer<DetalheModel> detalheModel(new DetalheModel());
    QScopedPointer<CUtil> util(new CUtil());

    qmlRegisterType<InternetClass>("com.rogerio.orders", 1, 0, "InternetClass");
    qmlRegisterType<ProcessadorClass>("com.rogerio.orders", 1, 0, "ProcessadorClass");
    qmlRegisterType<BancoCare>("com.rogerio.orders.models", 1, 0, "TrackModel");
    qmlRegisterType<DetalheModel>("com.rogerio.orders.models", 1, 0, "DetalheModel");

    QObject::connect(internet.data(), SIGNAL(resposta(QString)), processador.data(), SLOT(processador(QString)));
    QObject::connect(processador.data(), SIGNAL(informacaoProcessada(QString,QList<DetalheItem*>)), bancoCare.data(), SLOT(avaliar(QString,QList<DetalheItem*>)));    
    QObject::connect(bancoCare.data(), SIGNAL(trackAdd(TrackItem*)), trackModel.data(), SLOT(adicionarTrack(TrackItem*)));
    QObject::connect(bancoCare.data(), SIGNAL(detalheAdd(DetalheItem*)), detalheModel.data(), SLOT(detalheAdd(DetalheItem*)));    
    QObject::connect(bancoCare.data(), SIGNAL(limparDetalhes()), detalheModel.data(), SLOT(limpar()));
    QObject::connect(bancoCare.data(), SIGNAL(fimProcessamento(int)), detalheModel.data(), SLOT(refresh()));
    QObject::connect(trackModel.data(), SIGNAL(removerTrack(int)), bancoCare.data(), SLOT(removerTrack(int)));
    QObject::connect(trackModel.data(), SIGNAL(trackAtualizado(int,QString)), bancoCare.data(), SLOT(atualizarTrack(int,QString)));
    QObject::connect(trackModel.data(), SIGNAL(atualizarTrackInternet(QString)), internet.data(), SLOT(consultarSlot(QString)));

    QmlApplicationViewer viewer;    
    viewer.rootContext()->setContextProperty("internet", internet.data());    
    //viewer.rootContext()->setContextProperty("processador", processador.data());
    viewer.rootContext()->setContextProperty("detalheModel", detalheModel.data());
    viewer.rootContext()->setContextProperty("trackModel", trackModel.data());
    viewer.rootContext()->setContextProperty("bancoCare", bancoCare.data());
    viewer.rootContext()->setContextProperty("utilidade", util.data());
    //viewer.setAttribute(Qt::WA_LockPortraitOrientation);
    viewer.setMainQmlFile(QLatin1String("qml/CheckMyOrders/main.qml"));
    viewer.showExpanded();

    return app->exec();
}
